﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Integration;
using App.Utils;

namespace WebPortal
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtReturnUrl.Text = Request.QueryString["ReturnUrl"];
                ddlDomains.DataSource = SSOUtil.Context.GetDomains();
                ddlDomains.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string user = txtUser.Text;
            if (user.IndexOf("/")<0)
            {
                user = ddlDomains.SelectedValue + "/" + txtUser.Text;
            }

            if (!SSOUtil.Context.CheckUser(user, txtPassword.Text))
            {
                WebUtil.ExecuteClientScript(this.Page, "checkError();");
            }
            else
            {
                SSOUtil.Context.Login(user);
                if (!string.IsNullOrEmpty(txtReturnUrl.Text))
                {
                    if (txtReturnUrl.Text.IndexOf("Default.aspx") < 0)
                    {
                        Response.Redirect("Default.aspx?ReturnUrl=" + txtReturnUrl.Text);
                    }
                    else
                    {
                        Response.Redirect("Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }
    }
}